//
//  XSMianViewController.m
//  MeiLiYiChu
//
//  Created by mac on 16/2/23.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "XSMianViewController.h"
#import "XSButton.h"
#import "UICollectionViewWaterfallLayout.h"
#import "XSBrandChartViewController.h"
#import "XSMain.h"
#import "XSMianDetailViewController.h"
#import "XSScrollCollectionViewController.h"
#import "SVPullToRefresh.h"
#import "AppDelegate.h"
#import "XSJoinViewController.h"
#import "SearchViewController.h"
#import "XSShopCollectionViewController.h"
#import "XSSearchViewController.h"
@interface XSMianViewController ()<UIScrollViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate>
{
    UIScrollView * _smallScroll;
    NSArray * array;
    UILabel * lab;
    NSInteger  currentag;
    UIPageControl * page;
    NSTimer * _timer;
    NSInteger _speed;
    UICollectionView * _collection;
    UICollectionViewFlowLayout * layout;
    NSArray * btnArr;
    NSArray * titleArr;
    UIView * bigView;
    UIView * btnView;
    UIButton * imageView;
    UIView * view;
    UIView * brandView;
    UIView * payView;
    UILabel * hotLab;
    
    //上拉刷新 下拉加载
    NSInteger _page;
    NSString * nextMore;
    NSArray * urlArr;
    NSArray * labArr;
    UIView * topView;
    NSArray * navTitleArr;
    
    
    int row;
}
@end

@implementation XSMianViewController
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden =NO;
    
}
-(void)viewDidDisappear:(BOOL)animated
{
//    self.navigationController.navigationBarHidden =YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _page =1;
    
    navTitleArr =@[@"夺宝送豪礼！",@"品牌精选",@"护肤堪比微整形",@"跟着明星穿不出错",@"春装上新"];
    labArr =@[@"不是1元太少,是你还没来夺宝!1元就能买iPhone6,赶快来玩吧!",@"鞋子的春天",@"好的护肤品能有微整形的效果哟~",@"美,从模仿开始",@"春暖花开衣先知,不可错过的好店"];
    
    urlArr =@[@"http://vapi.yuike.com/1.0/product/object_products.php?type=activity&object_id=1453189918&count=40&mid=457465&jsonp_callback&sid=87b78c30e10ae09f6a842bf2ba4b8a06&yk_user_id=5123650&mid=457465&_=1456803593806",@"http://vapi.yuike.com/1.0/product/object_products.php?count=40&cursor=0&mid=457465&object_id=1456124442&sid=87b78c30e10ae09f6a842bf2ba4b8a06&type=activity&yk_appid=1&yk_cbv=2.9.3.1&yk_pid=1&yk_user_id=5123650",@"http://vapi.yuike.com/1.0/product/object_products.php?count=40&cursor=0&mid=457465&object_id=1456210287&sid=87b78c30e10ae09f6a842bf2ba4b8a06&type=activity&yk_appid=1&yk_cbv=2.9.3.1&yk_pid=1&yk_user_id=5123650",@"http://vapi.yuike.com/1.0/product/object_products.php?count=40&cursor=0&mid=457465&object_id=1455957722&sid=87b78c30e10ae09f6a842bf2ba4b8a06&type=activity&yk_appid=1&yk_cbv=2.9.3.1&yk_pid=1&yk_user_id=5123650",@"http://vapi.yuike.com/1.0/product/object_products.php?count=40&cursor=0&mid=457465&object_id=1456726690&sid=87b78c30e10ae09f6a842bf2ba4b8a06&type=activity&yk_appid=1&yk_cbv=2.9.3.1&yk_pid=1&yk_user_id=5123650"];
    btnArr =[NSArray arrayWithObjects:@"home_page_sign1",@"home_page_brand",@"image_homepage_hotlist",@"home_page_wx", nil];
    
    titleArr =@[@"商品分类",@"品牌排行",@"购物车",@"公众号"];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"alertViewCustom_right"] forBarMetrics:UIBarMetricsDefault];
    
    UIImageView * image =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 40)];
    image.image =[UIImage  imageNamed:@"home_page_titleimage"];
    self.navigationItem.titleView =image;

    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.bounds =CGRectMake(0, 0, 80, 30);
    button1.backgroundColor = [UIColor whiteColor];
    [button1 setTitle:@"我要加盟" forState:UIControlStateNormal];
    [button1 setTitleColor:[UIColor magentaColor] forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(join) forControlEvents:UIControlEventTouchUpInside];
    button1.layer.cornerRadius =5;
    button1.clipsToBounds =YES;
    UIBarButtonItem *item =[[UIBarButtonItem alloc]initWithCustomView:button1];
    self.navigationItem.leftBarButtonItem =item;
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    button2.bounds =CGRectMake(0, 0, 30, 20);
    [button2 setBackgroundImage:[UIImage imageNamed:@"home_page_title_search"] forState:UIControlStateNormal];
 
    [button2 addTarget:self action:@selector(search) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * item1 =[[UIBarButtonItem alloc]initWithCustomView:button2];
    self.navigationItem.rightBarButtonItem =item1;
  
    [XSHTTPManager getScrollPlistWithComplection:^(NSMutableArray *array1) {
        _scrollArr =array1;
        [self createBigView];
   
        [self createSmallScroll];
        
        [self createPageControl];
        
        [self createBtnView];
        
        [XSHTTPManager getBtnPlistWithComplection:^(NSMutableArray *leftArr, NSMutableArray *rightArr, NSString *imageUrl, NSMutableArray *titleArr1) {
            _leftArr =leftArr;
            _rightArr =rightArr;
            _imageUrl =imageUrl;
            _titleArray =titleArr1;
            
            [self createImageView];
            
            [self createHotView];
            
            [self createBrandView];
            
            [self createPayView];
            
            [self createCollentView];
            
            
            //下拉刷新;
            
            __weak   XSMianViewController * vc =self;
            [_collection addPullToRefreshWithActionHandler:^{
                //刷新第一页
                
                [vc refreshFrist];
            }];
            
            [_collection addInfiniteScrollingWithActionHandler:^{
                //上拉加载
                [vc refreshMove];
            }];

        } ];
      
    }];
    
    [self refreshCurrentView:1 :@"0"];
   
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor blueColor];
    [button setBackgroundImage:[UIImage imageNamed:@"go_top_btn"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(goTop) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    [self.view bringSubviewToFront:button];

    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.offset(20);
        make.bottom.offset(80);
        make.width.equalTo(@30);
        make.height.equalTo(@30);
//        make.left.equalTo(@(self.view.bounds.size.width-20-30));
    }];
    NSLog(@"%@--",button);
}
-(void)refreshFrist
{
     //刷新第一页
    [self refreshCurrentView:1 :@"0"];
    [_collection.pullToRefreshView stopAnimating];
    
}
-(void)refreshMove
{
    _page++;

//加载数据
    [self refreshCurrentView:_page : nextMore];
    //刷新更多页关闭旋转风叶
    [_collection.infiniteScrollingView stopAnimating];
}
-(void)refreshCurrentView:(NSInteger)pageg :(NSString *)currentPage
{
    [XSHTTPManager getCollectionPlistWithComplection:^(NSMutableArray *array2, NSString *next_cursor) {
        _collectionArr =array2;
        nextMore =next_cursor;

        
        [_collection reloadData];
    } :pageg seletBtnCurrent:currentPage];
}

#pragma mark - 必买清单
-(void)createPayView
{
    XSMain * main1 =_titleArray[3];
    
    UILabel * lab1 =[[UILabel alloc]init];
    
    lab1.text =main1.title;
    lab1.textColor =[UIColor magentaColor];
    lab1.font =[UIFont systemFontOfSize:15];
    CGSize  size =[lab1.text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}];
    
    lab1.frame =CGRectMake(10, brandView.frame.origin.y+brandView.frame.size.height, size.width, 40);
    
    
    
    [bigView addSubview:lab1];
    
    UILabel * lab2 =[[UILabel alloc]initWithFrame:CGRectMake(lab1.frame.size.width+lab1.frame.origin.x+10, brandView.frame.origin.y+brandView.frame.size.height+5, 80, 30)];
    
    lab2.text = main1.sub_title;
    
    lab2.font =[UIFont systemFontOfSize:13];
    lab2.alpha =.5;
    
    [bigView addSubview:lab2];

    payView =[[UIView alloc]initWithFrame:CGRectMake(0, lab1.frame.origin.y+lab1.frame.size.height, self.view.bounds.size.width, 200)];
    
    payView.backgroundColor =[UIColor whiteColor];
    
    [bigView addSubview:payView];
    
    
    XSMain * main =_leftArr[3];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, self.view.bounds.size.width/2, payView.frame.size.height);

    //图片上添加图片
    [button sd_setBackgroundImageWithURL:[NSURL URLWithString:main.Pic_url] forState:UIControlStateNormal];
    button.tag =920;
    [button addTarget:self action:@selector(mustPayClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [payView addSubview:button];
    
    for (int i = 0; i < 2; i ++)
    {
        XSMain * main =_rightArr[i+4];
        UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
        button2.frame = CGRectMake(self.view.bounds.size.width/2, i *brandView.frame.size.height/2, self.view.bounds.size.width/2, brandView.frame.size.height/2);
        //        button.backgroundColor = [UIColor blueColor];
        
        [button2 sd_setBackgroundImageWithURL:[NSURL URLWithString:main.Pic_url] forState:UIControlStateNormal];
        
        
        button2.tag =i+930;
        [button2 addTarget:self action:@selector(mustRightClick:) forControlEvents:UIControlEventTouchUpInside];
        [payView addSubview:button2];
    }

    hotLab =[[UILabel alloc]init];
    
    hotLab.text =@"热门商品";
    hotLab.textColor =[UIColor magentaColor];
    hotLab.font =[UIFont systemFontOfSize:15];
    CGSize  size1 =[hotLab.text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}];
    
    hotLab.frame =CGRectMake(10, payView.frame.origin.y+payView.frame.size.height, size1.width, 40);
    
    [bigView addSubview:hotLab];

    UIImageView * line =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, payView.frame.size.width, 2)];
    line.image =[UIImage imageNamed:@"line_activity"];
    [payView addSubview:line];
    UIImageView * line1 =[[UIImageView alloc]initWithFrame:CGRectMake(payView.frame.size.width/2, payView.frame.size.height/2, payView.frame.size.width/2, 2)];
    line1.image =[UIImage imageNamed:@"line_activity"];
    [payView addSubview:line1];
    UIImageView * line2 =[[UIImageView alloc]initWithFrame:CGRectMake(0, payView.frame.size.height, payView.frame.size.width, 2)];
    line2.image =[UIImage imageNamed:@"line_activity"];
    [payView addSubview:line2];
    
    UIImageView * line3 =[[UIImageView alloc]initWithFrame:CGRectMake(payView.frame.size.width/2, 0, 2, payView.frame.size.height)];
    line3.image =[UIImage imageNamed:@"activity_native_vline"];
    [payView addSubview:line3];
    
}
//必买清单
-(void)mustPayClick:(UIButton *)btn
{
    [self goShopRequsertWithUrl:@"234842" ];
}
-(void)mustRightClick:(UIButton *)btn
{
    if (btn.tag ==930) {
        [self goShopRequsertWithUrl:@"234871"];
    }
    else
    {
        [self goShopRequsertWithUrl:@"234882"];
    }
}
#pragma mark - 热门市场
//热门市场
-(void)createBrandView
{
    XSMain * main1 =_titleArray[2];
    
    UILabel * lab1 =[[UILabel alloc]init];
    
    lab1.text =main1.title;
    lab1.textColor =[UIColor magentaColor];
    lab1.font =[UIFont systemFontOfSize:15];
    CGSize  size =[lab1.text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}];
    
    lab1.frame =CGRectMake(10, view.frame.origin.y+view.frame.size.height, size.width, 40);



    [bigView addSubview:lab1];
    
    UILabel * lab2 =[[UILabel alloc]initWithFrame:CGRectMake(lab1.frame.size.width+lab1.frame.origin.x+10, view.frame.origin.y+view.frame.size.height+5, 80, 30)];
    
    lab2.text = main1.sub_title;
    
    lab2.font =[UIFont systemFontOfSize:13];
    lab2.alpha =.5;
    
    [bigView addSubview:lab2];
    
    brandView =[[UIView alloc]initWithFrame:CGRectMake(0, lab1.frame.origin.y+lab1.frame.size.height, self.view.bounds.size.width, 200)];
    
    brandView.backgroundColor =[UIColor whiteColor];
    
    [bigView addSubview:brandView];
    
    
    for (int i = 0; i < 2; i ++)
    {
        XSMain * main =_leftArr[i+1];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, i *brandView.frame.size.height/2, self.view.bounds.size.width/2, brandView.frame.size.height/2);
        button.backgroundColor = [UIColor blueColor];
        //图片上添加图片
        [button sd_setBackgroundImageWithURL:[NSURL URLWithString:main.Pic_url] forState:UIControlStateNormal];
        button.tag =i+900;
        [button addTarget:self action:@selector(brandLeftClick:) forControlEvents:UIControlEventTouchUpInside];
    
        [brandView addSubview:button];
    }
    
    for (int i = 0; i < 2; i ++)
    {
        XSMain * main =_rightArr[i+2];
        UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
        button2.frame = CGRectMake(self.view.bounds.size.width/2, i *brandView.frame.size.height/2, self.view.bounds.size.width/2, brandView.frame.size.height/2);
//        button.backgroundColor = [UIColor blueColor];
        
        [button2 sd_setBackgroundImageWithURL:[NSURL URLWithString:main.Pic_url] forState:UIControlStateNormal];
        

        button2.tag =i+910;
        [button2 addTarget:self action:@selector(brandRightClick:) forControlEvents:UIControlEventTouchUpInside];
        [brandView addSubview:button2];
    }
    UIImageView * line =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, brandView.frame.size.width, 2)];
    line.image =[UIImage imageNamed:@"line_activity"];
    [brandView addSubview:line];
    UIImageView * line2 =[[UIImageView alloc]initWithFrame:CGRectMake(0, brandView.frame.size.height, brandView.frame.size.width, 2)];
    line2.image =[UIImage imageNamed:@"line_activity"];
    [brandView addSubview:line2];
    UIImageView * line1 =[[UIImageView alloc]initWithFrame:CGRectMake(0, brandView.frame.size.height/2, brandView.frame.size.width, 2)];
    line1.image =[UIImage imageNamed:@"line_activity"];
    [brandView addSubview:line1];
    UIImageView * line3 =[[UIImageView alloc]initWithFrame:CGRectMake(brandView.frame.size.width/2, 0, 2, brandView.frame.size.height)];
    line3.image =[UIImage imageNamed:@"activity_native_vline"];
    [brandView addSubview:line3];

}
-(void)brandLeftClick:(UIButton *)btn
{
    if (btn.tag ==900)
    {
        [self goShopRequsertWithUrl:@"234878"];
    }
    else
    {
        [self goShopRequsertWithUrl:@"234854"];
    }
  
}
-(void)brandRightClick:(UIButton *)btn
{
    if (btn.tag ==910)
    {
        [self goShopRequsertWithUrl:@"234873"];
    }
    else
    {
        [self goShopRequsertWithUrl:@"234867"];
    }
}
#pragma mark - 套装上新
//春装上新
-(void)createHotView
{
    

    XSMain * main1 =_titleArray[1];
    
    UILabel * lab1 =[[UILabel alloc]init];


    lab1.text =main1.title;
    
    lab1.textColor =[UIColor magentaColor];
    lab1.font =[UIFont systemFontOfSize:15];
    CGSize  size =[lab1.text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}];
  
    lab1.frame =CGRectMake(10, imageView.frame.origin.y+imageView.frame.size.height, size.width, 40);
   
    [bigView addSubview:lab1];
    
    UILabel * lab2 =[[UILabel alloc]initWithFrame:CGRectMake(lab1.frame.size.width+lab1.frame.origin.x+10, imageView.frame.origin.y+imageView.frame.size.height+5, 80, 30)];

    lab2.text =main1.sub_title;
    
    lab2.font =[UIFont systemFontOfSize:13];
    lab2.alpha =.5;
    
    [bigView addSubview:lab2];
    
    
    view =[[UIView alloc]initWithFrame:CGRectMake(0, lab1.frame.origin.y+lab1.frame.size.height, self.view.bounds.size.width, 200)];
    
    view.backgroundColor =[UIColor whiteColor];
    
    [bigView addSubview:view];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, self.view.bounds.size.width/2, 200);

//    button.backgroundColor =[UIColor redColor];
  
    [button addTarget:self action:@selector(btnGo:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];

    UIImageView * imageView2 =[[UIImageView alloc]init];
 
    XSMain * main =_leftArr[0];
    
   
    [imageView2 sd_setImageWithURL:[NSURL URLWithString:main.taobao_pic_url]];
    
    [button addSubview:imageView2];
    
    [imageView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.offset(10);
        make.bottom.offset(-60);
        make.centerX.equalTo(button.mas_centerX);
        make.width.equalTo(@90);
    }];
    
    
    UILabel * lab3 =[[UILabel alloc]init];
    lab3.text =main.taobao_title;
    lab3.center =CGPointMake(button.center.x, button.frame.size.height-40);
    lab3.bounds =CGRectMake(0, 0, button.frame.size.width-40, 32);
   
    lab3.numberOfLines =0;
    lab3.font =[UIFont systemFontOfSize:13];
    [button addSubview:lab3];
    
    UILabel * lab4 =[[UILabel alloc]init];
    lab4.text =main.taobao_price;
    lab4.frame =CGRectMake(lab3.frame.origin.x, lab3.frame.origin.y+lab3.frame.size.height, 60, 12);
    lab4.font =[UIFont systemFontOfSize:12];
    lab4.alpha =.5;
    [button addSubview:lab4];
    
    
    UILabel * lab5 =[[UILabel alloc]init];
    lab5.text =main.taobao_promo_price;
    lab5.frame =CGRectMake(lab4.frame.origin.x, lab4.frame.origin.y+lab4.frame.size.height, 60, 12);
    lab5.textColor =[UIColor redColor];
    lab5.font =[UIFont systemFontOfSize:12];
    [button addSubview:lab5];
   
    for (int i =0;i <2; i++) {
        XSMain * main =_rightArr[i];
        
        UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame = CGRectMake(button.frame.size.width, i*button.frame.size.height/2, button.frame.size.width, button.frame.size.height/2);
        button1.tag =i+50;
    
    [button1 addTarget:self action:@selector(goShop:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button1];

    UIImageView * imageView3 =[[UIImageView alloc]init];
    [imageView3 sd_setImageWithURL:[NSURL URLWithString:main.taobao_pic_url]];
        
    imageView3.frame =CGRectMake(button1.frame.origin.x+20, 10+i *button1.frame.size.height, 40, 60);
        
    [view addSubview:imageView3];
        
        UILabel * lab6 =[[UILabel alloc]init];
        lab6.text =main.taobao_title;
        lab6.frame =CGRectMake(imageView3.frame.origin.x+imageView3.frame.size.width+10, imageView3.frame.origin.y+2, button1.frame.size.width-imageView3.frame.size.width-20-10, 40);
        lab6.font =[UIFont systemFontOfSize:13];
        lab6.numberOfLines =0;
        
        [view addSubview:lab6];
        
        
        UILabel * lab4 =[[UILabel alloc]init];
        lab4.text =main.taobao_price;
        lab4.frame =CGRectMake(imageView3.frame.origin.x+imageView3.frame.size.width+10, lab6.frame.origin.y+lab6.frame.size.height, 60, 14);
        lab4.font =[UIFont systemFontOfSize:12];
        lab4.alpha =.5;
        [button addSubview:lab4];
        
        
        UILabel * lab5 =[[UILabel alloc]init];
        lab5.text =main.taobao_promo_price;
        lab5.frame =CGRectMake(imageView3.frame.origin.x+imageView3.frame.size.width+10, lab6.frame.origin.y+lab6.frame.size.height+14, 60, 14);
        lab5.textColor =[UIColor redColor];
        lab5.font =[UIFont systemFontOfSize:12];
        [button addSubview:lab5];

    }
    UIImageView * line =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, view.frame.size.width, 2)];
    line.image =[UIImage imageNamed:@"line_activity"];
    [view addSubview:line];
    UIImageView * line1 =[[UIImageView alloc]initWithFrame:CGRectMake(view.frame.size.width/2, view.frame.size.height/2, view.frame.size.width/2, 2)];
    line1.image =[UIImage imageNamed:@"line_activity"];
    [view addSubview:line1];
    UIImageView * line2 =[[UIImageView alloc]initWithFrame:CGRectMake(0, view.frame.size.height, view.frame.size.width, 2)];
    line2.image =[UIImage imageNamed:@"line_activity"];
    [view addSubview:line2];
    
    UIImageView * line3 =[[UIImageView alloc]initWithFrame:CGRectMake(view.frame.size.width/2, 0, 2, view.frame.size.height)];
    line3.image =[UIImage imageNamed:@"activity_native_vline"];
    [view addSubview:line3];

}
-(void)btnGo:(UIButton *)btn
{
    
}
-(void)goShop:(UIButton *)btn
{
    
}

-(void)createImageView
{
    
    imageView =[[UIButton alloc]initWithFrame:CGRectMake(0, btnView.frame.origin.y+btnView.frame.size.height+10, self.view.bounds.size.width, 150)];
//    [imageView sd_setImageWithURL:[NSURL URLWithString:_imageUrl]];

    [imageView sd_setBackgroundImageWithURL:[NSURL URLWithString:_imageUrl] forState:UIControlStateNormal];
    [imageView addTarget:self action:@selector(shop:) forControlEvents:UIControlEventTouchUpInside];
    [bigView addSubview:imageView];
//    NSLog(@"%@--++",imageView)

}
#pragma mark - 0元抢购
//0元抢购
-(void)shop:(int)row2
{
    [self goShopRequsertWithUrl:@"234880"];
}
#pragma mark - 跳转店铺
-(void)goShopRequsertWithUrl:(NSString *)url
{
    [XSHTTPManager getZeroLabAndImageWithComplection:^(NSMutableArray *array2) {
        _zeroArr =array2;
        XSShop * shop =_zeroArr[0];
        
        UICollectionViewFlowLayout * layout2 =[[UICollectionViewFlowLayout alloc]init];
        layout2.itemSize =CGSizeMake(self.view.bounds.size.width/2-10, 300);
        layout2.minimumInteritemSpacing =10;
        layout2.minimumLineSpacing =10;
        
        XSShopCollectionViewController * shopVC =[[XSShopCollectionViewController alloc]initWithCollectionViewLayout:layout2];
        
        shopVC.labText =shop.title;
        shopVC.imageName =shop.logo_url;
        shopVC.url =url;

        
        shopVC.block =^(UIView *view2){
            layout2.sectionInset =UIEdgeInsetsMake(view2.frame.size.height, 5, 0, 5);
        };
        
        shopVC.collectionView.backgroundColor =backColor;
        
        shopVC.collectionView.frame =CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
        ;
        [shopVC.collectionView addSubview:shopVC.topView];
        [self addChildViewController:shopVC];
        self.hidesBottomBarWhenPushed =YES;
        [self.navigationController pushViewController:shopVC animated:YES];
        self.hidesBottomBarWhenPushed =NO;
    } withUrl:url];
    
}
-(void)createBigView
{
    self.automaticallyAdjustsScrollViewInsets =NO;
    
    bigView =[[UIView alloc]init];
   
    
    bigView.backgroundColor =[[UIColor alloc]initWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1];

    
}
-(void)goTop
{
    _smallScroll.scrollsToTop =YES;
    
}
//四个按钮
-(void)createBtnView
{
    btnView =[[UIView alloc]initWithFrame:CGRectMake(0, _smallScroll.frame.size.height, self.view.bounds.size.width, 100)];

    btnView.backgroundColor =backColor;
    
    [bigView addSubview:btnView];
    
    UIButton * lastBtn;
    for (int i = 0; i < 4; i++)
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = i+800;
        [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [btnView addSubview:button];
        
        UIImageView * imageView2 =[[UIImageView alloc]init];
        imageView2.image =[UIImage imageNamed:btnArr[i]];
        
        [btnView addSubview:imageView2];
        
        
        UILabel * lab2 =[[UILabel alloc]init];
        lab2.text =titleArr[i];
        lab2.font =[UIFont systemFontOfSize:15];
        lab2.textAlignment =NSTextAlignmentCenter;
        lab2.alpha =0.5;
        
        [btnView addSubview:lab2];
    
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(0);
            make.bottom.offset(0);
            
            
            if (lastBtn) {
                //存在的话说明第二个按钮起...
                make.left.equalTo(lastBtn.mas_right).with.offset(0);
                make.width.equalTo(lastBtn.mas_width);
            }
            else
            {
                //不存在说明第一个
                make.left.offset(0);
            }
            if (i ==3) {
                make.right.offset(0);
            }  
        }];
        
        [imageView2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(10);
            make.bottom.offset(-40);
            make.width.equalTo(@(100-20-30));
            make.centerX.equalTo(button.mas_centerX);
        }];
        
        [lab2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(imageView2.mas_bottom).offset(5);
            make.bottom.offset(-10);
            make.width.equalTo(@60);
            make.centerX.equalTo(button.mas_centerX);
        }];
        lastBtn =button;
        
        imageView.userInteractionEnabled =NO;
  
    }
    
    
}
-(void)btnClick:(UIButton *)btn
{
    NSLog(@"654546");
    switch (btn.tag)
    {
        case 800:
        {

        }
            break;
        case 801:
        {
            XSBrandChartViewController * chartVC =[[XSBrandChartViewController alloc]init];
            chartVC.delegate =self;
            
            self.hidesBottomBarWhenPushed =YES;
            [self.navigationController pushViewController:chartVC animated:YES];
            self.hidesBottomBarWhenPushed =NO;
        }
            break;
        case 802:
        {
            
        }
            break;
        case 803:
        {
            
        }
            break;
        default:
            break;
    }
}
-(void)createPageControl
{
    page =[[UIPageControl alloc]init];
    page.center =CGPointMake(bigView.center.x, _smallScroll.center.y+80);
    page.bounds =CGRectMake(0, 0, 150, 20);

    page.numberOfPages =5;

    
    [page setTintColor:[UIColor whiteColor]];
    [page setCurrentPageIndicatorTintColor:[UIColor magentaColor]];

    [bigView addSubview:page];
    
    
}
-(void)createSmallScroll
{

    
    _smallScroll =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 200)];
    _smallScroll.contentSize =CGSizeMake(_scrollArr.count *self.view.bounds.size.width, 200);
    _smallScroll.showsHorizontalScrollIndicator =NO;
    _smallScroll.showsVerticalScrollIndicator =NO;
    
    
    _smallScroll.backgroundColor =[UIColor yellowColor];
    
    _smallScroll.pagingEnabled =YES;
    _smallScroll.bounces =YES;
    _smallScroll.delegate =self;
    
    
    for (int i = 0; i< _scrollArr.count; i++)
    {
       
        UIButton* imageView1 =[[UIButton alloc]initWithFrame:CGRectMake(i *_smallScroll.frame.size.width, 0, _smallScroll.frame.size.width, 200)];
         XSMain * main =_scrollArr[i];
        imageView1.tag = i+1000;
        [imageView1 sd_setBackgroundImageWithURL:[NSURL URLWithString:main.Pic_url] forState:UIControlStateNormal];
        [imageView1 addTarget:self action:@selector(scrollClick:) forControlEvents:UIControlEventTouchUpInside];
        [_smallScroll addSubview:imageView1];
    }
    [bigView addSubview:_smallScroll];
    
    
    _timer =[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(changeImage) userInfo:nil repeats:YES];
    _speed =1;
}
#pragma mark - scroll点击事件
-(void)scrollClick:(UIButton *)btn
{
    NSInteger index =btn.tag-1000;
    if (index ==0)
    {
        [XSHTTPManager getFirstScrollPicWithComplection:^(NSMutableArray *array2) {
            _firstArray =array2;
            XSShop * shop =_firstArray[0];
            
            XSMain * main =_scrollArr[index];
            NSString * title =labArr[index];
            NSString * url =urlArr[index];
            
            UICollectionViewFlowLayout * layout1 =[[UICollectionViewFlowLayout alloc]init];
            layout1.itemSize =CGSizeMake(self.view.bounds.size.width/2-10, 300);
            XSScrollCollectionViewController * scollVC =[[XSScrollCollectionViewController alloc]initWithCollectionViewLayout:layout1];
            layout1.minimumLineSpacing =10;
            layout1.minimumInteritemSpacing =10;
            
            
            scollVC.block =^(UIView *view2){
                layout1.sectionInset =UIEdgeInsetsMake(view2.frame.size.height+view2.frame.origin.y, 5, 10, 5);
            };

            if (index ==0)
            {
                scollVC.imageName =shop.pic_url;
                scollVC.labTitle =shop.Description;
                scollVC.navTitle =shop.short_title;
            }
            else
            {
                scollVC.imageName =main.Pic_url;
                scollVC.labTitle =title;
                scollVC.navTitle =navTitleArr[index];
            }
            
            scollVC.url =url;
            scollVC.collectionView.backgroundColor =[[UIColor alloc]initWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1];
            scollVC.collectionView.frame =CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
            
            [scollVC.collectionView addSubview:scollVC.topView];
            
            [self addChildViewController:scollVC];
            self.hidesBottomBarWhenPushed =YES;
            [self.navigationController pushViewController:scollVC animated:YES];
            self.hidesBottomBarWhenPushed =NO;
        }];
    }
    else
    {
        XSShop * shop =_firstArray[0];
        
        XSMain * main =_scrollArr[index];
        NSString * title =labArr[index];
        NSString * url =urlArr[index];
        
        UICollectionViewFlowLayout * layout1 =[[UICollectionViewFlowLayout alloc]init];
        layout1.itemSize =CGSizeMake(self.view.bounds.size.width/2-10, 300);
        XSScrollCollectionViewController * scollVC =[[XSScrollCollectionViewController alloc]initWithCollectionViewLayout:layout1];
        layout1.minimumLineSpacing =10;
        layout1.minimumInteritemSpacing =10;
        
        
        scollVC.block =^(UIView *view2){
            layout1.sectionInset =UIEdgeInsetsMake(view2.frame.size.height+view2.frame.origin.y, 5, 10, 5);
        };
        
        
        if (index ==0)
        {
            scollVC.imageName =shop.pic_url;
            scollVC.labTitle =shop.Description;
            scollVC.navTitle =shop.short_title;
        }
        else
        {
            scollVC.imageName =main.Pic_url;
            scollVC.labTitle =title;
            scollVC.navTitle =navTitleArr[index];
        }
        
        scollVC.url =url;
        scollVC.collectionView.backgroundColor =[[UIColor alloc]initWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1];
        scollVC.collectionView.frame =CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
        
        [scollVC.collectionView addSubview:scollVC.topView];
        
        [self addChildViewController:scollVC];
        self.hidesBottomBarWhenPushed =YES;
        [self.navigationController pushViewController:scollVC animated:YES];
        self.hidesBottomBarWhenPushed =NO;

    }
    
    
}
#pragma mark -定时器事件
-(void)changeImage
{
    
    
     page.currentPage =_speed+page.currentPage;
    [_smallScroll setContentOffset:CGPointMake(_smallScroll.frame.size.width *page.currentPage , 0)];

    if (page.currentPage == 0)
    {
        _speed =1;
    }
    if (page.currentPage == 4)
    {
        _speed = -1;
    }
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    if (_timer)
    {
        //关闭定时器
        [_timer invalidate];
        _timer =nil;
    }
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (_timer ==nil)
    {
        _timer =[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(changeImage) userInfo:nil repeats:YES];
    }
    page.currentPage =_smallScroll.contentOffset.x/_scrollArr.count;
}
//自定义collection
-(void)createCollentView
{
    
    bigView.frame =CGRectMake(0, 64, self.view.bounds.size.width, hotLab.frame.origin.y+hotLab.frame.size.height);
    
    layout =[[UICollectionViewFlowLayout alloc]init];
    layout.itemSize =CGSizeMake(self.view.bounds.size.width/2-20, 300);

    layout.sectionInset =UIEdgeInsetsMake(bigView.frame.size.height+64, 10, 0, 10);
    
    layout.minimumInteritemSpacing =5;
    layout.minimumLineSpacing =10;

    _collection =[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-50) collectionViewLayout:layout];
    _collection.delegate =self;
    _collection.dataSource =self;
    _collection.backgroundColor =[[UIColor alloc]initWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1];
    [self.view addSubview:_collection];
    [_collection addSubview:bigView];

    [_collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
}
#pragma mark - collectionView
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{

    return _collectionArr.count;

}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell * cell =[_collection dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.contentView.backgroundColor =[UIColor whiteColor];
    
    UIImageView * imageView1 =(UIImageView *)[cell.contentView viewWithTag:cell.tag+10];
    if (imageView1==nil)
    {
        imageView1 =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, layout.itemSize.width, 240)];
        imageView1.tag =cell.tag+10;
        
        [cell.contentView addSubview:imageView1];
    }
    
    UILabel * lab1 =(UILabel *)[cell.contentView viewWithTag:cell.tag +200];
    if (lab1 ==nil)
    {
        lab1 =[[UILabel alloc]initWithFrame:CGRectMake(0, 240, layout.itemSize.width, 50)];
        lab1.tag =cell.tag +200;

        lab1.font =[UIFont systemFontOfSize:14];
        lab1.numberOfLines =0;
        
        [cell.contentView addSubview:lab1];
    }
    UILabel * lab2 =(UILabel *)[cell.contentView viewWithTag:cell.tag +500];
    if (lab2 ==nil)
    {
        lab2 =[[UILabel alloc]initWithFrame:CGRectMake(0, 280, layout.itemSize.width, 20)];
        lab2.tag =cell.tag +500;

        lab2.textColor =[UIColor magentaColor];
        [cell.contentView addSubview:lab2];
    }
   
    XSMain * main =_collectionArr[indexPath.row];
    [imageView1 sd_setImageWithURL:[NSURL URLWithString:main.taobao_pic_url]];
   
    lab1.text =main.taobao_title;
    lab2.text =[NSString stringWithFormat:@"%@ %@",main.money_symbol,main.taobao_selling_price];
    
    
    
    return cell;
}
#pragma mark - 按钮点击事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    XSMain * main =_collectionArr[indexPath.row];
    
    //跳转详情
    XSMianDetailViewController * detailVC =[[XSMianDetailViewController alloc]init];
    
    detailVC.url =main.taobao_url;
    detailVC.array =_collectionArr;
    detailVC.current =indexPath.row;
    
    
    self.hidesBottomBarWhenPushed =YES;
    [self.navigationController pushViewController:detailVC animated:YES];
    self.hidesBottomBarWhenPushed =NO;
//    _tabBar.barView.hidden =YES;
}
//加盟
-(void)join
{
    XSJoinViewController * joinVC =[[XSJoinViewController alloc]init];
    self.hidesBottomBarWhenPushed =YES;
    
    [self.navigationController pushViewController:joinVC animated:YES];
    self.hidesBottomBarWhenPushed =NO;
}
//搜索
-(void)search
{
     SearchViewController* searchVC =[[SearchViewController alloc]init];
    self.hidesBottomBarWhenPushed =YES;
    [self.navigationController pushViewController:searchVC animated:YES];
    self.hidesBottomBarWhenPushed =NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)name11 :(NSString *)url
{
    [self goShopRequsertWithUrl:url];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
